from django.contrib import admin
from bddjango.adminclass import BaseAdmin, BulkDeleteMixin
from pgZhSearch import RebuildIndexAdminMixin, TestSearchVectorMixin
from . import models
from tools.init_import_data_mixin import InitImportDataMixin


@admin.register(models.Question)
class Question(TestSearchVectorMixin.get_new(), BaseAdmin, BulkDeleteMixin, RebuildIndexAdminMixin, InitImportDataMixin):
    list_display = ['id', 'fen_lei_ming', 'wen_ti', 'is_active']
    # stop_field_ls = ['search_vector']
    # origin_str_list_display = True

    list_filter = ['fen_lei_ming', 'is_active']
    search_fields = ['id', 'fen_lei_ming', 'wen_ti']
    actions = ['bulk_delete', 'rebuild_index', 'init_import_data']

    # --- search_vector
    readonly_fields = ['search_vector']

    # --- TestSearchVectorMixin
    select_key = 'fen_lei_ming'
    default_search_keywords = '劳动纠纷'

    # --- InitImportDataMixin
    init_import_data_f_path = 'search/docs/问题表.xls'


@admin.register(models.TestModel)
class TestModel(TestSearchVectorMixin.get_new(), BaseAdmin, BulkDeleteMixin, RebuildIndexAdminMixin, InitImportDataMixin):
    actions = ['bulk_delete', 'rebuild_index', 'init_import_data']
    list_filter = ['is_active']
    list_display = ['id', 'title', 'content', 'search_vector', 'is_active']

    # --- search_vector
    readonly_fields = ['search_vector']

    # --- TestSearchVectorMixin
    default_search_keywords = '党校马克思主义哲学教学改革方向的探索'

    # --- InitImportDataMixin
    init_import_data_f_path = 'search/docs/测试表.xls'


# region # --- 创建`superuser`
from tools.db_utils import judge_db_is_migrating
from tools.create_user import create_user_if_not_exist
if not judge_db_is_migrating():
    username, passwd = 'admin', 'adminadmin'
    create_user_if_not_exist(username, passwd, superuser=True)
# endregion


